﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Excell_On_Services.Models;
using System.ComponentModel;
using Excell_On_Services.ViewModels;

namespace Excell_On_Services.Controllers
{
    public class AboutController : Controller
    {

        public IEOSRepository Repository { get; set; }

        public AboutController()
            : this(new EFEOSRepository())
        {

        }

        public AboutController(IEOSRepository repository)
        {
            Repository = repository;
        }

        //
        // GET: /About/
         [Authorize(Roles = "Administrators")]
        public ActionResult Index([DefaultValue(0)] int pageIndex)
        {
            //int departmentsPerPage = Configuration.Instance.PostPerPage;
            int departmentsPerPage = 6;
            return ShowAbouts(
                () => Repository.FindAboutInPage(pageIndex, departmentsPerPage),
                () => Repository.GetAboutCount(),
                pageIndex, "Index", departmentsPerPage
                );

        }
         [Authorize(Roles = "Administrators")]
        private ActionResult ShowAbouts(Func<IEnumerable<About>> filterFunc, Func<int> countFunc, int pageIndex, string actionName, int aboutPerPage)
        {
            var abouts = filterFunc();
            var aboutCount = countFunc();
            var model = new AboutDisplayModel
            {
                Abouts = abouts,
                PagerModel = new PagerModel
                {
                    CurrentPageIndex = pageIndex,
                    ItemsPerPage = aboutPerPage,
                    TotalItems = aboutCount,
                    PageUrlTemplate = Url.Action(actionName, new { pageIndex = PagerModel.PageSymbol })

                }
            };
            return View("Index", model);

        }
         [Authorize(Roles = "Administrators")]
        public ActionResult ShowCategory()
        {
            var model = Repository.FindAllCategory();
            return View("ShowCategory", model);
        }
         [Authorize(Roles = "Administrators")]
        public ActionResult CreateCategory()
        {
            return View();
        }


        //
        // GET: /Department/Details/5
         [Authorize(Roles = "Administrators")]
        public ActionResult Details(int id)
        {
            var about = Repository.FindAboutById(id);
            return View(about);
        }

        //
        // GET: /About/Create
         [Authorize(Roles = "Administrators")]
        public ActionResult Create()
        {
            var categories = Repository.FindAllCategory();
            ViewData["Categories"] = new SelectList(categories, "CategoryID", "CategoryName");
            
            return View();
        } 

        //
        // POST: /About/Create

        [HttpPost]
        [Authorize(Roles = "Administrators")]
        public ActionResult Create(AboutModel model)
        {
            var categories = Repository.FindAllCategory();
            ViewData["Categories"] = new SelectList(categories, "CategoryID", "CategoryName");
            try
            {
                // TODO: Add insert logic here
                if (!ModelState.IsValid)
                {
                    return View(model);
                }
                var about = new About
                {
                    Title = model.tilte,
                    Content = model.Content,
                    PublishedDate = model.publishedDate,
                    ModifiedDate = DateTime.Now
                };
                Repository.AddAbout(about);
                Repository.CommitChanges();
                return RedirectToAction("Details", new { id = about.AboutID});
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /About/Edit/5
        [Authorize(Roles = "Administrators")]
        public ActionResult Edit(int id)
        {
            try
            {
                var categories = Repository.FindAllCategory();
                ViewData["Categories"] = new SelectList(categories, "CategoryID", "CategoryName");

                var about = Repository.FindAboutById(id);

                var model = new AboutModel
                {
                    AboutID = about.AboutID,
                    CategoryID = (int)about.CategoryID,
                    tilte = about.Title,
                    Content = about.Content,
                    ModifiedDate = about.ModifiedDate,
                    publishedDate = about.PublishedDate,
                    Category = about.CategoryNew,
                    CheckActive = about.CheckActive,
                    CheckShow = about.CheckShow
                };
                return View(model);
            }
            catch (Exception ex)
            {
                return View("Index");
            }
            
        }

        //
        // POST: /About/Edit/5

        [HttpPost]
        [Authorize(Roles = "Administrators")]
        public ActionResult Edit(int id, AboutModel model)
        {
            var categories = Repository.FindAllCategory();
            ViewData["Categories"] = new SelectList(categories, "CategoryID", "CategoryName");
            
            try
            {
                if (!ModelState.IsValid)
                    return View(model);
                var item = Repository.FindAboutById(model.AboutID.Value);
                item.Title = model.tilte;
                item.PublishedDate = model.publishedDate;
                item.ModifiedDate = model.ModifiedDate;
                item.Content = model.Content;
                item.CheckActive = model.CheckActive;
                item.CheckShow = model.CheckShow;
                item.CategoryID = model.CategoryID;
                Repository.CommitChanges();
                return RedirectToAction("Details", new { id = item.AboutID });
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /About/Delete/5
        [Authorize(Roles = "Administrators")]
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /About/Delete/5

        [HttpPost]
        [Authorize(Roles = "Administrators")]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
